package org.jeecg.modules.demo.rksp.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.demo.rksp.entity.YlshRksp;
import org.jeecg.modules.demo.rksp.mapper.YlshRkspMapper;
import org.jeecg.modules.demo.rksp.service.IYlshRkspService;

import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: ylsh_rksp
 * @Author: jeecg-boot
 * @Date:   2021-12-15
 * @Version: V1.0
 */
@Api(tags="ylsh_rksp")
@RestController
@RequestMapping("/rksp/ylshRksp")
@Slf4j
public class YlshRkspController extends JeecgController<YlshRksp, IYlshRkspService> {
	@Autowired
	private IYlshRkspService ylshRkspService;
	@Autowired
	private YlshRkspMapper ylshRkspMapper;
	 @Autowired
	 RestTemplate restTemplate;
	/**
	 * 分页列表查询
	 *
	 * @param ylshRksp
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "ylsh_rksp-分页列表查询")
	@ApiOperation(value="ylsh_rksp-分页列表查询", notes="ylsh_rksp-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(YlshRksp ylshRksp,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		if (ylshRksp.getName() == null){
			return Result.OK("审核成功！");
		}
		Map<String,String> tb = ylshRkspMapper.selectTable(ylshRksp.getName());
		String mbdysjb = tb.get("mbdysjb");
		List<Map<String,Object>> tableAnnotation = ylshRkspMapper.tableAnnotation(mbdysjb);
		String[][] number=new String[tableAnnotation.size()][2];
		int i=0;
		for (Map<String, Object> map : tableAnnotation){
			System.out.println(map.keySet());
			for (String s : map.keySet()) {
				switch (s){
					case "Field": number[i][0] = map.get(s).toString();break;
					case "Comment": number[i][1] = map.get(s).toString();break;
				}
			}
			i++;
		}
		ArrayList<Object> header = new ArrayList<>();
		ArrayList<Object> content = new ArrayList<>();
		List<Map<String,Object>> list = ylshRkspMapper.selectContent(mbdysjb);
		for (int j = 1; j < number.length; j++) {
			Map<String,Object> map1 =new HashMap<>();
			map1.put("title",number[j][1]);
			map1.put("align","center");
			map1.put("dataIndex",number[j][0]);
			header.add(map1);
		}
		content.add(list);
		ArrayList<Object> mapContent = new ArrayList<>();
		mapContent.add(header);
		mapContent.add(content);
		mapContent.add(mbdysjb);
		return Result.OK(mapContent);
	}

	 @GetMapping(value = "/updateList")
	 public Result<?> updateList(String dHeader,String dSize){
		 List<String> ts = Arrays.asList(dSize.split(","));
		 ylshRkspMapper.updateList(dHeader,ts);
		 return Result.OK("审核成功！");
	 }

	/**
	 *   添加
	 *
	 * @param ylshRksp
	 * @return
	 */
	@AutoLog(value = "ylsh_rksp-添加")
	@ApiOperation(value="ylsh_rksp-添加", notes="ylsh_rksp-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody YlshRksp ylshRksp) {
		ylshRkspService.save(ylshRksp);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param ylshRksp
	 * @return
	 */
	@AutoLog(value = "ylsh_rksp-编辑")
	@ApiOperation(value="ylsh_rksp-编辑", notes="ylsh_rksp-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody YlshRksp ylshRksp) {
		ylshRkspService.updateById(ylshRksp);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ylsh_rksp-通过id删除")
	@ApiOperation(value="ylsh_rksp-通过id删除", notes="ylsh_rksp-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		ylshRkspService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "ylsh_rksp-批量删除")
	@ApiOperation(value="ylsh_rksp-批量删除", notes="ylsh_rksp-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.ylshRkspService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ylsh_rksp-通过id查询")
	@ApiOperation(value="ylsh_rksp-通过id查询", notes="ylsh_rksp-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		YlshRksp ylshRksp = ylshRkspService.getById(id);
		if(ylshRksp==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(ylshRksp);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param ylshRksp
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YlshRksp ylshRksp) {
        return super.exportXls(request, ylshRksp, YlshRksp.class, "ylsh_rksp");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YlshRksp.class);
    }

}
